<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="执行状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="runStatus">
              <j-dict-select-tag type="list" v-model="model.runStatus" dictCode="backup_status_code" placeholder="请选择执行状态" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备份文件" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="backFile">
              <j-upload v-model="model.backFile"   ></j-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备份命令" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dumpCmd">
              <a-input v-model="model.dumpCmd" placeholder="请输入备份命令"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="恢复命令" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="recoverCmd">
              <a-input v-model="model.recoverCmd" placeholder="请输入恢复命令"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="数据库名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dbName">
              <a-input v-model="model.dbName" placeholder="请输入数据库名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备份文件名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="backFileName">
              <a-input v-model="model.backFileName" placeholder="请输入备份文件名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="操作次数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="operateCount">
              <a-input-number v-model="model.operateCount" placeholder="请输入操作次数" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="文件大小" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="fileSize">
              <a-input v-model="model.fileSize" placeholder="请输入文件大小"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备份时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dumpTime">
              <j-date placeholder="请选择备份时间"  v-model="model.dumpTime" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="恢复时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="recoverTime">
              <j-date placeholder="请选择恢复时间"  v-model="model.recoverTime" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-input v-model="model.remark" placeholder="请输入备注"  ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'TigerDataBackupForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/util/tigerDataBackup/add",
          edit: "/util/tigerDataBackup/edit",
          queryById: "/util/tigerDataBackup/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>